unit GetConf;
interface
  uses sysutils;
  type
   scif=set of char;
   Atargets=array[0..14] of boolean;
   Aterrain=record
    a,d,v:integer;
   end;
   Aunits=record
    a,d:integer;
   end;
   Runitcfg=record
    name:string[15];
    iter:array[1..3] of byte;
    iterp:array[1..3] of byte;
    Atack:byte;
    Defence:byte;
    Move:byte;
    vision:byte;
    FuelAmmo:word;
    Cost:word;
    Targets:Atargets;
    Forest:Aterrain;
    Mount:Aterrain;
    Boloto:Aterrain;
    Desert:Aterrain;
    Arctic:Aterrain;
    Town:Aterrain;
    Infantry:Aunits;
    Gun:Aunits;
    AirGun:Aunits;
    Tank:Aunits;
    Supplies:Aunits;
    Fighter:Aunits;
    Bomber:Aunits;
    Submarine:Aunits;
    Transport:Aunits;
    Tanker:Aunits;
    Cruiser:Aunits;
    BattleShip:Aunits;
    Destroyer:Aunits;
    Deminer:Aunits;
    Carrier:Aunits;
   end;

   TunitCfg=object
    cfgs:array[0..14] of Runitcfg;
    Procedure LoadFromTextFile(name:string);
    Procedure SaveToFile(name:string);
    Procedure LoadFromFile(name:string);
    Function GetCost(typ,lev:byte):word;
    Function GetAttack(typ,typ1,tera:byte):integer;
    Function GetDefence(typ,typ1,tera:byte):integer;
    Function GetAtBonU(typ,typ1:byte):integer;
    Function GetAtBonT(typ,tera:byte):integer;
    Function GetDefBonU(typ,typ1:byte):integer;
    Function GetDefBonT(typ,tera:byte):integer;

   end;

  var
   Hucfg:TunitCfg;

implementation

Function Tunitcfg.GetAtBonU;
var
 na,at:integer;
begin
na:=0;
case typ1 of
  0:na:=cfgs[typ].infantry.a;
  1:na:=cfgs[typ].gun.a;
  2:na:=cfgs[typ].airgun.a;
  3:na:=cfgs[typ].tank.a;
  4:na:=cfgs[typ].supplies.a;
  5:na:=cfgs[typ].fighter.a;
  6:na:=cfgs[typ].bomber.a;
  7:na:=cfgs[typ].submarine.a;
  8:na:=cfgs[typ].transport.a;
  9:na:=cfgs[typ].Tanker.a;
  10:na:=cfgs[typ].Cruiser.a;
  11:na:=cfgs[typ].BattleShip.a;
  12:na:=cfgs[typ].Destroyer.a;
  13:na:=cfgs[typ].DeMiner.a;
  14:na:=cfgs[typ].Carrier.a;
 end;
 result:=na;
end;

Function tunitcfg.GetCost;
var
 i:integer;
 cost:word;
begin
 cost:=cfgs[typ].cost;
 for i:=1 to lev-1 do begin
  cost:=round(cost+(cost/100)*20);
 end;
 result:=cost;
end;

function Tunitcfg.GetAtBonT;
var
 na:integer;
begin
 na:=0;
 case tera of
  0:na:=0;
  1:na:=cfgs[typ].forest.a;
  2:na:=cfgs[typ].Desert.a;
  3:na:=cfgs[typ].Arctic.a;
  4:na:=cfgs[typ].Town.a;
  5:na:=cfgs[typ].mount.a;
  6:na:=cfgs[typ].Boloto.a;
 end;
 result:=na;
end;


Function Tunitcfg.GetDefBonU;
var
 na,at:integer;
begin
na:=0;
case typ1 of
  0:na:=cfgs[typ].infantry.d;
  1:na:=cfgs[typ].gun.d;
  2:na:=cfgs[typ].airgun.d;
  3:na:=cfgs[typ].tank.d;
  4:na:=cfgs[typ].supplies.d;
  5:na:=cfgs[typ].fighter.d;
  6:na:=cfgs[typ].bomber.d;
  7:na:=cfgs[typ].submarine.d;
  8:na:=cfgs[typ].transport.d;
  9:na:=cfgs[typ].Tanker.d;
  10:na:=cfgs[typ].Cruiser.d;
  11:na:=cfgs[typ].BattleShip.d;
  12:na:=cfgs[typ].Destroyer.d;
  13:na:=cfgs[typ].DeMiner.d;
  14:na:=cfgs[typ].Carrier.d;
 end;
 result:=na;
end;

function Tunitcfg.GetDefBonT;
var
 na:integer;
begin
 na:=0;
 case tera of
  0:na:=0;
  1:na:=cfgs[typ].forest.d;
  2:na:=cfgs[typ].Desert.d;
  3:na:=cfgs[typ].Arctic.d;
  4:na:=cfgs[typ].Town.d;
  5:na:=cfgs[typ].mount.d;
  6:na:=cfgs[typ].Boloto.d;
 end;
 result:=na;
end;



Function Tunitcfg.GetAttack;
var
 na,at:integer;
begin
 at:=cfgs[typ].atack;
 na:=0;
 case typ1 of
  0:na:=cfgs[typ].infantry.a;
  1:na:=cfgs[typ].gun.a;
  2:na:=cfgs[typ].airgun.a;
  3:na:=cfgs[typ].tank.a;
  4:na:=cfgs[typ].supplies.a;
  5:na:=cfgs[typ].fighter.a;
  6:na:=cfgs[typ].bomber.a;
  7:na:=cfgs[typ].submarine.a;
  8:na:=cfgs[typ].transport.a;
  9:na:=cfgs[typ].Tanker.a;
  10:na:=cfgs[typ].Cruiser.a;
  11:na:=cfgs[typ].BattleShip.a;
  12:na:=cfgs[typ].Destroyer.a;
  13:na:=cfgs[typ].DeMiner.a;
  14:na:=cfgs[typ].Carrier.a;
 end;
 at:=at+na;
 na:=0;
 case tera of
  0:na:=0;
  1:na:=cfgs[typ].forest.a;
  2:na:=cfgs[typ].Desert.a;
  3:na:=cfgs[typ].Arctic.a;
 end;
 at:=at+na;
 result:=at;
end;

Function Tunitcfg.GetDefence;
var
 na,at:integer;
begin
 at:=cfgs[typ].defence;
 na:=0;
 case typ1 of
  0:na:=cfgs[typ].infantry.d;
  1:na:=cfgs[typ].gun.d;
  2:na:=cfgs[typ].airgun.d;
  3:na:=cfgs[typ].tank.d;
  4:na:=cfgs[typ].supplies.d;
  5:na:=cfgs[typ].fighter.d;
  6:na:=cfgs[typ].bomber.d;
  7:na:=cfgs[typ].submarine.d;
  8:na:=cfgs[typ].transport.d;
  9:na:=cfgs[typ].Tanker.d;
  10:na:=cfgs[typ].Cruiser.d;
  11:na:=cfgs[typ].BattleShip.d;
  12:na:=cfgs[typ].Destroyer.d;
  13:na:=cfgs[typ].DeMiner.d;
  14:na:=cfgs[typ].Carrier.d;
 end;
 at:=at+na;
 na:=0;
 case tera of
  0:na:=0;
  1:na:=cfgs[typ].forest.d;
  2:na:=cfgs[typ].Desert.d;
  3:na:=cfgs[typ].Arctic.d;
 end;
 at:=at+na;
 result:=at;
end;



Procedure TUnitcfg.LoadFromFile;
var
 f:file;
 k,e,i:integer;
 len:word;
 it:word;
 fl:boolean;
begin
 assignfile(f,name);reset(f,sizeof(cfgs));
 blockread(f,cfgs,1);
 closefile(f);
 for i:=0 to 14 do begin
  len:=cfgs[i].move;
  it:=len div 3;
  cfgs[i].iter[1]:=it;
  cfgs[i].iter[2]:=it;
  cfgs[i].iter[3]:=len-(it+it);
  if (cfgs[i].iter[3]-cfgs[i].iter[1])=2 then begin
   cfgs[i].iter[1]:=cfgs[i].iter[1]+1;
   cfgs[i].iter[3]:=cfgs[i].iter[3]-2;
   cfgs[i].iter[2]:=cfgs[i].iter[2]+1;
  end;
  repeat
  fl:=true;
   for e:=1 to 2 do begin
    if cfgs[i].iter[e]<cfgs[i].iter[e+1] then begin
      k:=cfgs[i].iter[e];
      cfgs[i].iter[e]:=cfgs[i].iter[e+1];
      cfgs[i].iter[e+1]:=k;
      fl:=false;
    end;
   end;
  until fl;
  cfgs[i].iterp[1]:=cfgs[i].iter[1];
  cfgs[i].iterp[2]:=cfgs[i].iter[1]+cfgs[i].iter[2];
  cfgs[i].iterp[3]:=cfgs[i].move;
 end;
end;


Procedure Tunitcfg.LoadFromTextFile;
var
 f:Text;
 s:string;
 e,len,it,i,k:integer;
 cif:scif;
 fl:boolean;
Function GetStr(st:string):string;
var
 i:integer;
 re:string;
 fl:Boolean;
begin
 re:='';fl:=false;
 for i:=1 to length(st) do begin
  if st[i]='=' then fl:=true;
  if (fl=true) and (st[i] <>'=') then re:=re+st[i];
 end;
 result:=re;
end;


Function Get2Coma(var ss:string):integer;
var
 i:integer;
 ts:string;
begin
 i:=0;
 repeat
  i:=i+1;
 until (ss[i]=',') or (i=length(ss));
 if ss[i]=',' then ts:=copy(ss,1,i-1) else ts:=copy(ss,1,i);
 result:=strtoint(ts);
 delete(ss,1,i);
end;


Function GetNum(st:string):integer;
var
i:integer;
 re:string;
 fl:Boolean;
begin
 cif:=['0','1','2','3','4','5','6','7','8','9'];
 re:='';fl:=false;
 for i:=1 to length(st) do begin
  if st[i]='=' then fl:=true;
  if (fl=true) and (st[i] in cif) then re:=re+st[i];
 end;
 if re='' then result:=0 else result:=strtoint(re);
end;

Function GetTargets(st:string):ATargets;
var
 i:integer;
 s:string;
 fl:boolean;
begin
  s:='';fl:=false;
  for i:=1 to length(st) do begin
   if st[i]='=' then fl:=true;
   if (fl=true) and (st[i]<>'=') then s:=s+st[i]
  end;
  for i:=1 to 15 do if s[i]='1' then result[i]:=true else result[i]:=false;
end;

Function GetTerra(st:string):Aterrain;
var
 i:integer;
 s:string;
 fl:boolean;
begin
  s:='';fl:=false;
  for i:=1 to length(st) do begin
   if st[i]='=' then fl:=true;
   if (fl=true) and (st[i]<>'=') then s:=s+st[i]
  end;
  result.a:=get2coma(s);
  result.d:=get2coma(s);
  result.v:=get2coma(s);
end;

Function GetUnit(st:string):Aunits;
var
 i:integer;
 s:string;
 fl:boolean;
begin
  s:='';fl:=false;
  for i:=1 to length(st) do begin
   if st[i]='=' then fl:=true;
   if (fl=true) and (st[i]<>'=') then s:=s+st[i]
  end;
  result.a:=get2coma(s);
  result.d:=get2coma(s);
end;

begin
 fillchar(cfgs,sizeof(cfgs),0);
 assignfile(f,name);
 reset(f);k:=-1;
 readln(f,s);
 repeat
   if s='' then else
   if s[1]='[' then begin
    k:=k+1;
    readln(f,s);cfgs[k].name:=getstr(s);
    readln(f,s);cfgs[k].atack:=getnum(s);
    readln(f,s);cfgs[k].Defence:=getnum(s);
    readln(f,s);cfgs[k].Move:=getnum(s);
    readln(f,s);cfgs[k].Vision:=getnum(s);
    readln(f,s);cfgs[k].FuelAmmo:=getnum(s);
    readln(f,s);cfgs[k].Cost:=getnum(s);
    readln(f,s);cfgs[k].Targets:=getTargets(s);
    readln(f,s);cfgs[k].Forest:=getTerra(s);
    readln(f,s);cfgs[k].mount:=getTerra(s);
    readln(f,s);cfgs[k].Boloto:=getTerra(s);
    readln(f,s);cfgs[k].Town:=getTerra(s);
    readln(f,s);cfgs[k].Infantry:=getUnit(s);
    readln(f,s);cfgs[k].Gun:=getUnit(s);
    readln(f,s);cfgs[k].AirGun:=getUnit(s);
    readln(f,s);cfgs[k].Tank:=getUnit(s);
    readln(f,s);cfgs[k].Supplies:=getUnit(s);
    readln(f,s);cfgs[k].Fighter:=getUnit(s);
    readln(f,s);cfgs[k].Bomber:=getUnit(s);
    readln(f,s);cfgs[k].Submarine:=getUnit(s);
    readln(f,s);cfgs[k].Transport:=getUnit(s);
    readln(f,s);cfgs[k].Tanker:=getUnit(s);
    readln(f,s);cfgs[k].Cruiser:=getUnit(s);
    readln(f,s);cfgs[k].BattleShip:=getUnit(s);
    readln(f,s);cfgs[k].Destroyer:=getUnit(s);
    readln(f,s);cfgs[k].DeMiner:=getUnit(s);
    readln(f,s);cfgs[k].Carrier:=getUnit(s);
   end;
   readln(f,s);
 until eof(f);
 closefile(f);
 for i:=0 to 14 do begin
  len:=cfgs[i].move;
  it:=len div 3;
  cfgs[i].iter[1]:=it;
  cfgs[i].iter[2]:=it;
  cfgs[i].iter[3]:=len-(it+it);
  if (cfgs[i].iter[3]-cfgs[i].iter[1])=2 then begin
   cfgs[i].iter[1]:=cfgs[i].iter[1]+1;
   cfgs[i].iter[3]:=cfgs[i].iter[3]-2;
   cfgs[i].iter[2]:=cfgs[i].iter[2]+1;
  end;
  repeat
  fl:=true;
   for e:=1 to 2 do begin
    if cfgs[i].iter[e]<cfgs[i].iter[e+1] then begin
      k:=cfgs[i].iter[e];
      cfgs[i].iter[e]:=cfgs[i].iter[e+1];
      cfgs[i].iter[e+1]:=k;
      fl:=false;
    end;
   end;
  until fl
 end;
 SaveTofile('armies.dat');
end;

Procedure Tunitcfg.SaveToFile;
var
 f:File;
 ff:text;
 i:integer;
begin
 assignfile(f,name);rewrite(f,sizeof(cfgs));
 blockwrite(f,cfgs,1);
 closefile(f);
end;
end.
